package cn.chenxing.utils;

import cn.chenxing.domain.dto.TrackData;
import cn.chenxing.domain.dto.TrackLine;
import cn.chenxing.domain.dto.TrackPoint;
import cn.chenxing.domain.dto.TrackTable;
import cn.chenxing.domain.threadlocal.ThreadData;
import cn.chenxing.domain.util.DateUtil;
import com.jfoenix.controls.JFXComboBox;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 常用的公用的工具类
 * @Author maogen.ymg
 * @Date 2020/4/15 0:07
 */
public class CommonUtil {

    private CommonUtil() {}

    /**
     * 小数点保留n位
     * @param d 数值
     * @param newScale 保留位数
     * @return Double
     */
    public static double decimalValue(double d, int newScale) {
        BigDecimal bd = BigDecimal.valueOf(d).setScale(newScale, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    }

    /**
     * TrackLineList转换为TrackTableList
     * @return TrackTableList
     */
    public static List<TrackTable> getTrackTableData() {
        List<TrackTable> trackTableList = new ArrayList<>();

        // 用户数据
        List<TrackLine> trackLineList = ThreadData.get().getTrackData().getTrackLineList();

        for(TrackLine trackLine : trackLineList) {
            long userId = trackLine.getId();

            for(TrackPoint trackPoint : trackLine.getTrackPointList()) {
                TrackTable trackTable = new TrackTable();
                trackTable.setId(userId)
                        .setLongitude(trackPoint.getLongitude())
                        .setLatitude(trackPoint.getLatitude())
                        .setTime(DateUtil.timeStamp2Date(trackPoint.getTimeStamp(), null))
                        .setSpeed(trackPoint.getSpeed());
                trackTableList.add(trackTable);
            }

        }
        return trackTableList;
    }

    /**
     *  获取表格数据---根据用户ID筛选
     *
     * @param userStr 用户ID
     * @param isSave 是否保存为公有数据
     * @return List<TrackTable>
     */
    public static List<TrackTable> getTableDataByUserId(String userStr, boolean isSave) {
        List<TrackTable> trackTableList = new ArrayList<>();
        List<TrackLine> trackLineList = ThreadData.get().getTrackData().getTrackLineList();
        for(TrackLine trackLine : trackLineList) {
            if(userStr.equals(String.valueOf(trackLine.getId()))) {
                for(TrackPoint trackPoint : trackLine.getTrackPointList()) {
                    TrackTable trackTable = new TrackTable();
                    trackTable.setId(trackLine.getId())
                            .setLongitude(trackPoint.getLongitude())
                            .setLatitude(trackPoint.getLatitude())
                            .setTime(DateUtil.timeStamp2Date(trackPoint.getTimeStamp(), null))
                            .setSpeed(trackPoint.getSpeed());
                    trackTableList.add(trackTable);
                }
            }

        }
        if(isSave) {
            // 保存为公有数据
            TrackData trackData = ThreadData.get().getTrackData();
            trackData.setTrackLineList(trackLineList);
            ThreadData.get().setTrackData(trackData);
        }
        return trackTableList;
    }

    /**
     * 更新用户表
     * @param userList 用户列表下拉框
     * @param needAll 是否需要添加“所有用户”
     */
    public static void updateList(JFXComboBox<String> userList, boolean needAll) {
        if(needAll) {
            userList.getItems().add("所有用户");
            // 防止底下判断为空
            userList.setValue(userList.getItems().get(0));
        }
        if(ThreadData.get().getTrackData() != null) {
            List<TrackLine> trackLineList = ThreadData.get().getTrackData().getTrackLineList();
            for(TrackLine trackLine : trackLineList) {
                userList.getItems().add(trackLine.getId() + "");
            }
            userList.setValue(userList.getItems().get(0));
        }
    }

    /**
     * 计算两轨迹点之间真实距离
     * @return 公里
     */
    public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
        // 维度
        double lat1 = (Math.PI / 180) * latitude1;
        double lat2 = (Math.PI / 180) * latitude2;

        // 经度
        double lon1 = (Math.PI / 180) * longitude1;
        double lon2 = (Math.PI / 180) * longitude2;

        // 地球半径
        double R = 6371;

        // 两点间距离 km
        return Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R;

    }

}
